/**
 * Created by joustany on 17/11/22.
 */
$(function () {

})
var count = 60;
var InterValObj
var params = JSON.parse(window.localStorage.user);
var redirect = JSON.parse(decodeURIComponent(params['redirect']));
var isPhone = window.localStorage.phone
// console.log($.urlGet().page)
$(document).on('click','.show_encode',function () {
    "use strict"
    var access_token = redirect.token
    var phone = $(".phone_box")[0].value
    var phoneVerifg = /^1[34578]\d{9}$/;
    if(!phoneVerifg.test($(".phone_box")[0].value))
    {
        layer.open({
            content: '请输入正确手机号码'
            ,skin: 'msg'
            ,time: 2 //2秒后自动关闭
        });
    }else {
        $('.show_encode').addClass('hide_encode')
        $.ajax({
            url: configApi('/sms/send'),
            data: {
                phone:phone,
                type: 'region',
                access_token: access_token
            },
            dataType: 'json',
            type: "POST",
        }).done(function (res) {
            // alert(res.message)
            if (res.success){
                $('.encode_box').focus()
                $(".show_encode").removeClass('show_encode');
                $(".hide_encode").addClass('hide_encode');
                InterValObj = window.setInterval(SetRemainTime, 1000)
                layer.open({
                    content: res.message
                    ,skin: 'msg'
                    ,time: 2 //2秒后自动关闭
                });
                }else {
                    layer.open({
                        content: res.message
                        ,skin: 'msg'
                        ,time: 2 //2秒后自动关闭
                    });
            }
        }).fail(function () {
            layer.open({
                content: "服务器异常,请退出重试"
                ,skin: 'msg'
                ,time: 2 //2秒后自动关闭
            });
        })
    }
})
function SetRemainTime() {
    console.log(count)
    if (count == 0) {
        //清除样式恢复原状
        window.clearInterval(InterValObj);//停止计时器
        $('.hide_encode').addClass('show_encode')
        $('.show_encode').removeClass('hide_encode')
        $(".show_encode").html("发送验证码");
        count = 60
    }
    else {
        count--;
        $(".hide_encode").html(count + "秒后重试");
    }
}
$(document).on('click','.login',function () {
    var phone = $(".phone_box")[0].value
    var code = $(".encode_box")[0].value
    console.log(code)
    //如果验证码是4位发送请求
    if(code.length == 4){
        $('.loding').show()
        $.ajax({
            url: configApi('/user/region'),
            data: {
                phone:phone,
                code: code,
                access_token: redirect.token
            },
            dataType: 'json',
            type: "POST",
        }).done(function (res) {
            $('.loding').hide()
            //停止倒计时
            var data = res.data
            window.clearInterval(InterValObj)
            if (res.success){
                // if ($.isEmptyObject(data)){
                //     window.location.href = './me.html'
                // }else {
                //     if (redirect.phone == ''){
                //         // window.location.href = './topUp.html'
                //     }else {
                //         // var desc = data.desc,
                //         //     money = data.money,
                //         //     title = data.title
                //         // window.location.href = './me.html?desc=' + desc + '&money=' + money + '&title='+ title +''
                //     }
                // }
                // if (isPhone == ''){
                //     window.localStorage.phone = phone
                //     window.location.href = './topUp.html'
                // }else {
                console.log(res)
                if ($.urlGet().record){
                    window.localStorage.phone = phone
                    if ($.isEmptyObject(data)){
                        window.location.href = './me.html'
                    }else {
                        var desc = data.desc,
                            money = data.money,
                            title = data.title
                        window.location.href = './me.html?desc=' + desc + '&money=' + money + '&title='+ title +''
                    }
                }else {
                    window.localStorage.phone = phone
                        if ($.isEmptyObject(data)){
                            window.location.href = './topUp.html'
                        }else {
                            window.localStorage.top = '1'
                            window.location.href = './topUp.html'
                        }
                }
            }else {
                layer.open({
                    content: res.message
                    ,skin: 'msg'
                    ,time: 2 //2秒后自动关闭
                });
            }
        }).fail(function () {
            layer.open({
                content: "服务器异常,请退出重试"
                ,skin: 'msg'
                ,time: 2 //2秒后自动关闭
            });
        })
    }else {
        $('.loding').hide()
        layer.open({
            content: '您输入的验证码有误'
            ,skin: 'msg'
            ,time: 2 //2秒后自动关闭
        });
    }
})
